﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using dmoz.Entities;
using dmoz.Data;

#endregion

namespace dmoz.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="CategoryProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class CategoryProviderBaseCore : EntityProviderBase<dmoz.Entities.Category, dmoz.Entities.CategoryKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByIdResourceFromResourcePerCategory
		
		/// <summary>
		///		Gets Category objects from the datasource by idResource in the
		///		ResourcePerCategory table. Table Category is related to table Resource
		///		through the (M:N) relationship defined in the ResourcePerCategory table.
		/// </summary>
		/// <param name="idResource"></param>
		/// <returns>Returns a typed collection of Category objects.</returns>
		public TList<Category> GetByIdResourceFromResourcePerCategory(System.Int32 idResource)
		{
			int count = -1;
			return GetByIdResourceFromResourcePerCategory(null,idResource, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets dmoz.Entities.Category objects from the datasource by idResource in the
		///		ResourcePerCategory table. Table Category is related to table Resource
		///		through the (M:N) relationship defined in the ResourcePerCategory table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="idResource"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Category objects.</returns>
		public TList<Category> GetByIdResourceFromResourcePerCategory(System.Int32 idResource, int start, int pageLength)
		{
			int count = -1;
			return GetByIdResourceFromResourcePerCategory(null, idResource, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Category objects from the datasource by idResource in the
		///		ResourcePerCategory table. Table Category is related to table Resource
		///		through the (M:N) relationship defined in the ResourcePerCategory table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="idResource"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Category objects.</returns>
		public TList<Category> GetByIdResourceFromResourcePerCategory(TransactionManager transactionManager, System.Int32 idResource)
		{
			int count = -1;
			return GetByIdResourceFromResourcePerCategory(transactionManager, idResource, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets Category objects from the datasource by idResource in the
		///		ResourcePerCategory table. Table Category is related to table Resource
		///		through the (M:N) relationship defined in the ResourcePerCategory table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="idResource"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Category objects.</returns>
		public TList<Category> GetByIdResourceFromResourcePerCategory(TransactionManager transactionManager, System.Int32 idResource,int start, int pageLength)
		{
			int count = -1;
			return GetByIdResourceFromResourcePerCategory(transactionManager, idResource, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Category objects from the datasource by idResource in the
		///		ResourcePerCategory table. Table Category is related to table Resource
		///		through the (M:N) relationship defined in the ResourcePerCategory table.
		/// </summary>
		/// <param name="idResource"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Category objects.</returns>
		public TList<Category> GetByIdResourceFromResourcePerCategory(System.Int32 idResource,int start, int pageLength, out int count)
		{
			
			return GetByIdResourceFromResourcePerCategory(null, idResource, start, pageLength, out count);
		}


		/// <summary>
		///		Gets Category objects from the datasource by idResource in the
		///		ResourcePerCategory table. Table Category is related to table Resource
		///		through the (M:N) relationship defined in the ResourcePerCategory table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="idResource"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Category objects.</returns>
		public abstract TList<Category> GetByIdResourceFromResourcePerCategory(TransactionManager transactionManager,System.Int32 idResource, int start, int pageLength, out int count);
		
		#endregion GetByIdResourceFromResourcePerCategory
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, dmoz.Entities.CategoryKey key)
		{
			return Delete(transactionManager, key.Id);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="id">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 id)
		{
			return Delete(null, id);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 id);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Category_Category key.
		///		FK_Category_Category Description: 
		/// </summary>
		/// <param name="parentId"></param>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		public dmoz.Entities.TList<Category> GetByParentId(System.Int32? parentId)
		{
			int count = -1;
			return GetByParentId(parentId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Category_Category key.
		///		FK_Category_Category Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="parentId"></param>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		/// <remarks></remarks>
		public dmoz.Entities.TList<Category> GetByParentId(TransactionManager transactionManager, System.Int32? parentId)
		{
			int count = -1;
			return GetByParentId(transactionManager, parentId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_Category_Category key.
		///		FK_Category_Category Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="parentId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		public dmoz.Entities.TList<Category> GetByParentId(TransactionManager transactionManager, System.Int32? parentId, int start, int pageLength)
		{
			int count = -1;
			return GetByParentId(transactionManager, parentId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Category_Category key.
		///		fkCategoryCategory Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="parentId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		public dmoz.Entities.TList<Category> GetByParentId(System.Int32? parentId, int start, int pageLength)
		{
			int count =  -1;
			return GetByParentId(null, parentId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Category_Category key.
		///		fkCategoryCategory Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="parentId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		public dmoz.Entities.TList<Category> GetByParentId(System.Int32? parentId, int start, int pageLength,out int count)
		{
			return GetByParentId(null, parentId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Category_Category key.
		///		FK_Category_Category Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="parentId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		public abstract dmoz.Entities.TList<Category> GetByParentId(TransactionManager transactionManager, System.Int32? parentId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the Label_Category_FK1 key.
		///		Label_Category_FK1 Description: 
		/// </summary>
		/// <param name="idLabel"></param>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		public dmoz.Entities.TList<Category> GetByIdLabel(System.Int32 idLabel)
		{
			int count = -1;
			return GetByIdLabel(idLabel, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the Label_Category_FK1 key.
		///		Label_Category_FK1 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="idLabel"></param>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		/// <remarks></remarks>
		public dmoz.Entities.TList<Category> GetByIdLabel(TransactionManager transactionManager, System.Int32 idLabel)
		{
			int count = -1;
			return GetByIdLabel(transactionManager, idLabel, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the Label_Category_FK1 key.
		///		Label_Category_FK1 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="idLabel"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		public dmoz.Entities.TList<Category> GetByIdLabel(TransactionManager transactionManager, System.Int32 idLabel, int start, int pageLength)
		{
			int count = -1;
			return GetByIdLabel(transactionManager, idLabel, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the Label_Category_FK1 key.
		///		labelCategoryFk1 Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="idLabel"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		public dmoz.Entities.TList<Category> GetByIdLabel(System.Int32 idLabel, int start, int pageLength)
		{
			int count =  -1;
			return GetByIdLabel(null, idLabel, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the Label_Category_FK1 key.
		///		labelCategoryFk1 Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="idLabel"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		public dmoz.Entities.TList<Category> GetByIdLabel(System.Int32 idLabel, int start, int pageLength,out int count)
		{
			return GetByIdLabel(null, idLabel, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the Label_Category_FK1 key.
		///		Label_Category_FK1 Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="idLabel"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of dmoz.Entities.Category objects.</returns>
		public abstract dmoz.Entities.TList<Category> GetByIdLabel(TransactionManager transactionManager, System.Int32 idLabel, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override dmoz.Entities.Category Get(TransactionManager transactionManager, dmoz.Entities.CategoryKey key, int start, int pageLength)
		{
			return GetById(transactionManager, key.Id, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key Category_PK index.
		/// </summary>
		/// <param name="id"></param>
		/// <returns>Returns an instance of the <see cref="dmoz.Entities.Category"/> class.</returns>
		public dmoz.Entities.Category GetById(System.Int32 id)
		{
			int count = -1;
			return GetById(null,id, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the Category_PK index.
		/// </summary>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="dmoz.Entities.Category"/> class.</returns>
		public dmoz.Entities.Category GetById(System.Int32 id, int start, int pageLength)
		{
			int count = -1;
			return GetById(null, id, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the Category_PK index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="dmoz.Entities.Category"/> class.</returns>
		public dmoz.Entities.Category GetById(TransactionManager transactionManager, System.Int32 id)
		{
			int count = -1;
			return GetById(transactionManager, id, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the Category_PK index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="dmoz.Entities.Category"/> class.</returns>
		public dmoz.Entities.Category GetById(TransactionManager transactionManager, System.Int32 id, int start, int pageLength)
		{
			int count = -1;
			return GetById(transactionManager, id, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the Category_PK index.
		/// </summary>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="dmoz.Entities.Category"/> class.</returns>
		public dmoz.Entities.Category GetById(System.Int32 id, int start, int pageLength, out int count)
		{
			return GetById(null, id, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the Category_PK index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="dmoz.Entities.Category"/> class.</returns>
		public abstract dmoz.Entities.Category GetById(TransactionManager transactionManager, System.Int32 id, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a dmoz.Entities.TList&lt;Category&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="dmoz.Entities.TList&lt;Category&gt;"/></returns>
		public static dmoz.Entities.TList<Category> Fill(IDataReader reader, dmoz.Entities.TList<Category> rows, int start, int pageLength)
		{
		// advance to the starting row
		for (int i = 0; i < start; i++)
		{
			if (!reader.Read())
			return rows; // not enough rows, just return
		}
		for (int i = 0; i < pageLength; i++)
		{
			if (!reader.Read())
			break; // we are done
			string key = null;
			
			dmoz.Entities.Category c = null;
			if (DataRepository.Provider.UseEntityFactory)
			{
			key = new System.Text.StringBuilder("Category")
			.Append("|").Append((reader.IsDBNull(((int)dmoz.Entities.CategoryColumn.Id - 1))?(int)0:(System.Int32)reader[((int)dmoz.Entities.CategoryColumn.Id - 1)]).ToString()).ToString();
			c = EntityManager.LocateOrCreate<Category>(
			key.ToString(), // EntityTrackingKey
			"Category",  //Creational Type
			DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
			DataRepository.Provider.EnableEntityTracking); // Track this entity?
			}
			else
			{
			c = new dmoz.Entities.Category();
			}
			
			if (!DataRepository.Provider.EnableEntityTracking ||
			c.EntityState == EntityState.Added ||
			(DataRepository.Provider.EnableEntityTracking &&
			((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
			(DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
								c.EntityState == EntityState.Changed)))))
						{
			c.SuppressEntityEvents = true;
			c.Id = (System.Int32)reader["id"];
			c.OriginalId = c.Id;
			c.IdLabel = (System.Int32)reader["idLabel"];
			c.ParentId = reader.IsDBNull(reader.GetOrdinal("parentId")) ? null : (System.Int32?)reader["parentId"];
			c.EntityTrackingKey = key;
			c.AcceptChanges();
			c.SuppressEntityEvents = false;
			}
			rows.Add(c);
		}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="dmoz.Entities.Category"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="dmoz.Entities.Category"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, dmoz.Entities.Category entity)
		{
			if (!reader.Read()) return;
			
			entity.Id = (System.Int32)reader["id"];
			entity.OriginalId = (System.Int32)reader["id"];
			entity.IdLabel = (System.Int32)reader["idLabel"];
			entity.ParentId = reader.IsDBNull(reader.GetOrdinal("parentId")) ? null : (System.Int32?)reader["parentId"];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="dmoz.Entities.Category"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="dmoz.Entities.Category"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, dmoz.Entities.Category entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.Id = (System.Int32)dataRow["id"];
			entity.OriginalId = (System.Int32)dataRow["id"];
			entity.IdLabel = (System.Int32)dataRow["idLabel"];
			entity.ParentId = Convert.IsDBNull(dataRow["parentId"]) ? null : (System.Int32?)dataRow["parentId"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="dmoz.Entities.Category"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">dmoz.Entities.Category Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		internal override void DeepLoad(TransactionManager transactionManager, dmoz.Entities.Category entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region ParentIdSource	
			if (CanDeepLoad(entity, "Category|ParentIdSource", deepLoadType, innerList) 
				&& entity.ParentIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.ParentId ?? (int)0);
				Category tmpEntity = EntityManager.LocateEntity<Category>(EntityLocator.ConstructKeyFromPkItems(typeof(Category), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ParentIdSource = tmpEntity;
				else
					entity.ParentIdSource = DataRepository.CategoryProvider.GetById(transactionManager, (entity.ParentId ?? (int)0));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ParentIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ParentIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.CategoryProvider.DeepLoad(transactionManager, entity.ParentIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ParentIdSource

			#region IdLabelSource	
			if (CanDeepLoad(entity, "LocalizedName|IdLabelSource", deepLoadType, innerList) 
				&& entity.IdLabelSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.IdLabel;
				LocalizedName tmpEntity = EntityManager.LocateEntity<LocalizedName>(EntityLocator.ConstructKeyFromPkItems(typeof(LocalizedName), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.IdLabelSource = tmpEntity;
				else
					entity.IdLabelSource = DataRepository.LocalizedNameProvider.GetById(transactionManager, entity.IdLabel);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'IdLabelSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.IdLabelSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.LocalizedNameProvider.DeepLoad(transactionManager, entity.IdLabelSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion IdLabelSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetById methods when available
			
			#region CategoryCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<Category>|CategoryCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CategoryCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.CategoryCollection = DataRepository.CategoryProvider.GetByParentId(transactionManager, entity.Id);

				if (deep && entity.CategoryCollection.Count > 0)
				{
					deepHandles.Add("CategoryCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<Category>) DataRepository.CategoryProvider.DeepLoad,
						new object[] { transactionManager, entity.CategoryCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region ResourcePerCategoryCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ResourcePerCategory>|ResourcePerCategoryCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ResourcePerCategoryCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ResourcePerCategoryCollection = DataRepository.ResourcePerCategoryProvider.GetByIdCategory(transactionManager, entity.Id);

				if (deep && entity.ResourcePerCategoryCollection.Count > 0)
				{
					deepHandles.Add("ResourcePerCategoryCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ResourcePerCategory>) DataRepository.ResourcePerCategoryProvider.DeepLoad,
						new object[] { transactionManager, entity.ResourcePerCategoryCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region ResourceCollection_From_ResourcePerCategory
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Resource>|ResourceCollection_From_ResourcePerCategory", deepLoadType, innerList))
			{
				entity.ResourceCollection_From_ResourcePerCategory = DataRepository.ResourceProvider.GetByIdCategoryFromResourcePerCategory(transactionManager, entity.Id);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ResourceCollection_From_ResourcePerCategory' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ResourceCollection_From_ResourcePerCategory != null)
				{
					deepHandles.Add("ResourceCollection_From_ResourcePerCategory",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Resource >) DataRepository.ResourceProvider.DeepLoad,
						new object[] { transactionManager, entity.ResourceCollection_From_ResourcePerCategory, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the dmoz.Entities.Category object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">dmoz.Entities.Category instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">dmoz.Entities.Category Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		internal override bool DeepSave(TransactionManager transactionManager, dmoz.Entities.Category entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region ParentIdSource
			if (CanDeepSave(entity, "Category|ParentIdSource", deepSaveType, innerList) 
				&& entity.ParentIdSource != null)
			{
				DataRepository.CategoryProvider.Save(transactionManager, entity.ParentIdSource);
				entity.ParentId = entity.ParentIdSource.Id;
			}
			#endregion 
			
			#region IdLabelSource
			if (CanDeepSave(entity, "LocalizedName|IdLabelSource", deepSaveType, innerList) 
				&& entity.IdLabelSource != null)
			{
				DataRepository.LocalizedNameProvider.Save(transactionManager, entity.IdLabelSource);
				entity.IdLabel = entity.IdLabelSource.Id;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<Delegate, object> deepHandles = new Dictionary<Delegate, object>();

			#region ResourceCollection_From_ResourcePerCategory>
			if (CanDeepSave(entity.ResourceCollection_From_ResourcePerCategory, "List<Resource>|ResourceCollection_From_ResourcePerCategory", deepSaveType, innerList))
			{
				if (entity.ResourceCollection_From_ResourcePerCategory.Count > 0 || entity.ResourceCollection_From_ResourcePerCategory.DeletedItems.Count > 0)
				{
					DataRepository.ResourceProvider.Save(transactionManager, entity.ResourceCollection_From_ResourcePerCategory); 
					deepHandles.Add(
						(DeepSaveHandle<Resource>) DataRepository.ResourceProvider.DeepSave,
						new object[] { transactionManager, entity.ResourceCollection_From_ResourcePerCategory, deepSaveType, childTypes, innerList }
					);
				}
			}
			#endregion 
	
			#region List<Category>
				if (CanDeepSave(entity.CategoryCollection, "List<Category>|CategoryCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(Category child in entity.CategoryCollection)
					{
						if(child.ParentIdSource != null)
							child.ParentId = child.ParentIdSource.Id;
						else
							child.ParentId = entity.Id;

					}

					if (entity.CategoryCollection.Count > 0 || entity.CategoryCollection.DeletedItems.Count > 0)
					{
						DataRepository.CategoryProvider.Save(transactionManager, entity.CategoryCollection);
						
						deepHandles.Add(
							(DeepSaveHandle< Category >) DataRepository.CategoryProvider.DeepSave,
							new object[] { transactionManager, entity.CategoryCollection, deepSaveType, childTypes, innerList }
						);
					}
				} 
			#endregion 
				
	
			#region List<ResourcePerCategory>
				if (CanDeepSave(entity.ResourcePerCategoryCollection, "List<ResourcePerCategory>|ResourcePerCategoryCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ResourcePerCategory child in entity.ResourcePerCategoryCollection)
					{
						if(child.IdCategorySource != null)
							child.IdCategory = child.IdCategorySource.Id;
						else
							child.IdCategory = entity.Id;

						//Handle right table of ResourceCollection_From_ResourcePerCategory
						if(child.IdCategorySource != null)
						{
								child.IdCategory = child.IdCategorySource.Id;

						}

						//Handle right table of ResourceCollection_From_ResourcePerCategory
						if(child.IdResourceSource != null)
						{
								child.IdResource = child.IdResourceSource.Id;

						}

					}

					if (entity.ResourcePerCategoryCollection.Count > 0 || entity.ResourcePerCategoryCollection.DeletedItems.Count > 0)
					{
						DataRepository.ResourcePerCategoryProvider.Save(transactionManager, entity.ResourcePerCategoryCollection);
						
						deepHandles.Add(
							(DeepSaveHandle< ResourcePerCategory >) DataRepository.ResourcePerCategoryProvider.DeepSave,
							new object[] { transactionManager, entity.ResourcePerCategoryCollection, deepSaveType, childTypes, innerList }
						);
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region CategoryChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>dmoz.Entities.Category</c>
	///</summary>
	public enum CategoryChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Category</c> at ParentIdSource
		///</summary>
		[ChildEntityType(typeof(Category))]
		Category,
			
		///<summary>
		/// Composite Property for <c>LocalizedName</c> at IdLabelSource
		///</summary>
		[ChildEntityType(typeof(LocalizedName))]
		LocalizedName,
	
		///<summary>
		/// Collection of <c>Category</c> as OneToMany for CategoryCollection
		///</summary>
		[ChildEntityType(typeof(TList<Category>))]
		CategoryCollection,

		///<summary>
		/// Collection of <c>Category</c> as OneToMany for ResourcePerCategoryCollection
		///</summary>
		[ChildEntityType(typeof(TList<ResourcePerCategory>))]
		ResourcePerCategoryCollection,

		///<summary>
		/// Collection of <c>Category</c> as ManyToMany for ResourceCollection_From_ResourcePerCategory
		///</summary>
		[ChildEntityType(typeof(TList<Resource>))]
		ResourceCollection_From_ResourcePerCategory,
	}
	
	#endregion CategoryChildEntityTypes
	
	#region CategoryFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;CategoryColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Category"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class CategoryFilterBuilder : SqlFilterBuilder<CategoryColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the CategoryFilterBuilder class.
		/// </summary>
		public CategoryFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the CategoryFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public CategoryFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the CategoryFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public CategoryFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion CategoryFilterBuilder
	
	#region CategoryParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;CategoryColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Category"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class CategoryParameterBuilder : ParameterizedSqlFilterBuilder<CategoryColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the CategoryParameterBuilder class.
		/// </summary>
		public CategoryParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the CategoryParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public CategoryParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the CategoryParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public CategoryParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion CategoryParameterBuilder
} // end namespace
